package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.FleeInfo;

public class FleeAction extends Action {

    public FleeAction( FleeInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        if ( !getEntityController().getCombatController().isInCombat() ) {
            getEntityController().tx( "You are not fighting." );
            return CommandExecutionResult.OK;
        }

        if ( getInfo().isStopFleeing() ) {
            if ( !getEntityController().getCombatController().isFleeing() ) {
                getEntityController().tx( "You are not fleeing!" );
                return CommandExecutionResult.OK;
            }
            else {
                getEntityController().tx( "You stop trying to flee." );
                getEntityController().getCombatController().stopFleeThread();
                return CommandExecutionResult.OK;
            }
        }

        checkForRunningCraft();

        getEntityController().getEchoes().txStartFleeing();

        getEntityController().getCombatController().startFleeThread( getInfo().getDirection() );

        return CommandExecutionResult.OK;
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

    @Override
    public FleeInfo getInfo() {
        return (FleeInfo) super.getInfo();
    }

}
